package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.pojo.dto.BrandCategoryAndNewDTO;
import cn.tedu.csmall.product.pojo.entity.BrandCategory;
import cn.tedu.csmall.product.pojo.vo.BrandCategoryStandardVO;
import cn.tedu.csmall.product.service.IBrandCategoryService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class BrandCategoryServiceImpl implements IBrandCategoryService, ServiceCode {
    @Autowired
    BrandCategoryMapper brandCategoryMapper;
    public BrandCategoryServiceImpl(){
       log.debug("创建业务类：BrandCategoryServiceImpl");
    }
    @Override
    public void andNew(BrandCategoryAndNewDTO brandCategoryAndNewDTO) {
        log.debug("开始添加数据!参数：{}",brandCategoryAndNewDTO);
        LocalDateTime now=LocalDateTime.now();
        BrandCategory brandCategory=new BrandCategory();
        BeanUtils.copyProperties(brandCategoryAndNewDTO,brandCategory);
        brandCategory.setGemCreate(now);
        brandCategory.setGemModified(now);
        int insert = brandCategoryMapper.insert(brandCategory);
        if (insert!=1){
            String message="添加数据失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }
    @Override
    public void delete(Long id) {
    log.debug("开始处理删除功能!参数:{}",id);
        BrandCategoryStandardVO brandCategoryMapperBCS = brandCategoryMapper.getBCS(id);
        log.debug("开始处理删除，删除数据id:{},删除数据:{}",id,brandCategoryMapperBCS);
        if (brandCategoryMapperBCS==null){
            String message="删除数据失败！"+id+"不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int row=brandCategoryMapper.deleteById(id);
        if (row!=1){
            String message="删除数据失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
}
